import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from '@vue/reactivity'

// interface UserTalbeParam {
//   getUrl?:string
//   delUrl?:string
//   addUrl?:string
//   updateUrl?:string
// }

export default function useTable () {
  const table = ref({
    list: [],
    total: 0,
    params: {
      pageSize: 10,
      pageNum: 1,
    },
    multipleSelection: [],
  })
  async function getList (fn:(args:any[])=>Promise<any>, params) {
    const data = await fn(params)
    console.log('getList', data)
    table.value.list = data.rows
    table.value.total = data.count
  }

  function handleDelete (fn:(args:any[])=>Promise<any>, data, msg?:string) {
    return new Promise((resolve, reject) => {
      ElMessageBox.confirm(msg || '此操作将删除该条数据，是否继续？', {
        type: 'error',
      })
        .then(async () => {
          await fn(data)
          ElMessage.success('删除成功')
          resolve(true)
        })
        .catch((e) => {
        // ElMessage.info('取消操作')
          console.error(`${fn.name} catch`)
          reject(e)
        })
    })
  }
  return {
    table,
    handleDelete,
    getList,
  }
}
